package com.ziyueyuwen.xiniao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ziyueyuwen.xiniao.base.exception.nullpoint.CourseNotExistException;
import com.ziyueyuwen.xiniao.bean.ZyClassInfo;
import com.ziyueyuwen.xiniao.bean.ZyCourseInfo;
import com.ziyueyuwen.xiniao.bean.ao.CourseSaveAO;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.query.CourseQuery;
import com.ziyueyuwen.xiniao.bean.vo.ZyCourseVO;
import com.ziyueyuwen.xiniao.dao.ZyClassInfoMapper;
import com.ziyueyuwen.xiniao.dao.ZyCourseInfoMapper;
import com.ziyueyuwen.xiniao.service.ClassService;
import com.ziyueyuwen.xiniao.service.CourseService;
import com.ziyueyuwen.xiniao.util.PageDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wancheng  on 2018/12/17.
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    ZyCourseInfoMapper zyCourseInfoMapper;
    @Autowired
    ZyClassInfoMapper zyClassInfoMapper;
    @Autowired
    ClassService classService;

    @Override
    public ZyCourseInfo saveCourse(CourseSaveAO course) {
        ZyCourseInfo zyCourseInfo = new ZyCourseInfo();
        zyCourseInfo.setCourseName(course.getCourseName());
        zyCourseInfo.setGrade(course.getGrade());
        zyCourseInfo.setLessonCount(course.getLessonNum());
        zyCourseInfo.setCreateTime(new Date());
        zyCourseInfo.setYear(course.getYear());
        zyCourseInfo.setQuarter(course.getQuarter());
        zyCourseInfo.setDescription(course.getDescription());
        zyCourseInfo.setImageUrl(course.getImageUrl());
        zyCourseInfoMapper.insertSelective(zyCourseInfo);
        return zyCourseInfo;
    }

    @Override
    public ZyCourseInfo updateCourse(CourseSaveAO course) throws Exception {
        ZyCourseInfo original = zyCourseInfoMapper.selectByPrimaryKey(course.getCourseId());
        if (original == null) {
            throw new Exception("课程为null");
        }
        ZyCourseInfo zyCourseInfo = new ZyCourseInfo();
        zyCourseInfo.setId(course.getCourseId());
        zyCourseInfo.setCourseName(course.getCourseName());
        zyCourseInfo.setGrade(course.getGrade());
        zyCourseInfo.setLessonCount(course.getLessonNum());
        zyCourseInfo.setYear(course.getYear());
        zyCourseInfo.setQuarter(course.getQuarter());
        zyCourseInfo.setDescription(course.getDescription());
        zyCourseInfo.setImageUrl(course.getImageUrl());

        classService.editClassDescriptionAndImage(course.getCourseId(), course.getDescription(), course.getImageUrl());
        zyCourseInfoMapper.updateByPrimaryKeySelective(zyCourseInfo);
        return zyCourseInfo;
    }

    @Override
    public ZyCourseInfo getByPreKey(Integer courseId) {
        return zyCourseInfoMapper.selectByPrimaryKey(courseId);
    }

    @Override
    public PageData<ZyCourseVO> getAllZyCourse(CourseQuery query, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy(" create_time desc ");
        List<ZyCourseVO> res = zyCourseInfoMapper.getCourseByCourseQuery(query);
        PageInfo<ZyCourseVO> pageInfo = new PageInfo<>(res);
        PageData<ZyCourseVO> pageData = PageDataUtil.getPageData(pageInfo);
        List<ZyCourseVO> data = pageData.getData();
        data.forEach(
                zyCourseVO -> {
                    if (zyCourseVO.getGrade() != null) {
                        zyCourseVO.setGradeValue(zyCourseVO.getGrade().getDescription());
                    }
                    if (zyCourseVO.getQuarter() != null) {
                        zyCourseVO.setQuarterValue(zyCourseVO.getQuarter().getDescription());
                    }
                    ZyClassInfo param = new ZyClassInfo();
                    param.setCourseId(zyCourseVO.getId());
                    param.setIfDelete(false);
                    List<ZyClassInfo> classInfos = zyClassInfoMapper.select(param);
                    zyCourseVO.setClassInfos(classInfos);
                }
        );
        return pageData;
    }

    @Override
    public PageData<ZyCourseVO> getCourseForQuestion(CourseQuery query) {
        PageHelper.startPage(query.getPage(), query.getRows());
        PageHelper.orderBy(" create_time desc ");
        List<ZyCourseVO> res = zyCourseInfoMapper.getCourseByCourseQuery(query);
        PageInfo<ZyCourseVO> pageInfo = new PageInfo<>(res);
        PageData<ZyCourseVO> pageData = PageDataUtil.getPageData(pageInfo);
        return pageData;
    }

    @Override
    public ZyCourseVO getByCourseId(Integer courseId) {
        ZyCourseVO res = zyCourseInfoMapper.getByCourseId(courseId);
        if (res.getGrade() != null) {
            res.setGradeValue(res.getGrade().getDescription());
        }
        return res;
    }

    @Override
    public void updateCourseLessonCountIdentity(Integer courseId) throws CourseNotExistException {
        if(getByPreKey(courseId) == null){
            throw new CourseNotExistException();
        }
        zyCourseInfoMapper.updateCourseLessonCountIdentity(courseId);
    }

    @Override
    public Boolean deleteEmptyCourseByCourseId(Integer courseId) {
        return zyCourseInfoMapper.deleteEmptyCourseByCourseId(courseId);
    }
}
